package com.example.timerecorder;

import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class AlterRecordActivity extends AppCompatActivity {

    private static final String TAG = "AlterRecordActivity";
    public static final int REQUEST_CODE_ALTER_TIME = 7;
    public static final int RESULT_CODE_ALTER_TIME = 8;
    public static final int REQUEST_CODE_ALTER_TAG = 9;
    public static final int RESULT_CODE_ALTER_TAG = 10;

    private EditText titleText;
    private EditText detailText;
    private TextView currentTime;
    private ImageView alterTimeImage;
    private TextView currentTag;
    private ImageView alterTagImage;
    private Button saveBtn;

    String currentDateStr, currentTimeStr, currentTitleStr, currentDetailStr, currentTagStr;
    int currentId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alter_record);

        //返回
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        titleText = findViewById(R.id.addTitle);
        detailText = findViewById(R.id.addDetail);
        currentTime = findViewById(R.id.currentTime);
        alterTimeImage = findViewById(R.id.alterTimeImage);
        currentTag = findViewById(R.id.currentTag);
        alterTagImage = findViewById(R.id.alterTagImage);
        saveBtn = findViewById(R.id.saveAlterBtn);

        //使用intent接收当前记录内容
        Intent intent = getIntent();
        currentId = intent.getIntExtra("alterId",0);
        currentDateStr = intent.getStringExtra("alterDate");
        currentTimeStr = intent.getStringExtra("alterTime");
        currentTitleStr = intent.getStringExtra("alterTitle");
        currentDetailStr = intent.getStringExtra("alterDetail");
        currentTagStr = intent.getStringExtra("alterTag");
        String dateStr = currentDateStr + " " + currentTimeStr;

        //设置界面显示内容
        currentTime.setText(dateStr);
        titleText.setText(currentTitleStr);
        detailText.setText(currentDetailStr);
        currentTag.setText(currentTagStr);

        //修改时间，跳转页面
        alterTimeImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //打开新窗口
                Intent selectTime = new Intent(AlterRecordActivity.this, SelectTimeActivity.class);
                selectTime.putExtra("current_date", currentDateStr);
                selectTime.putExtra("current_time", currentTimeStr);
                selectTime.putExtra("flag", "alter");
                startActivityForResult(selectTime, REQUEST_CODE_ALTER_TIME);
            }
        });

        //添加tag，跳转页面
        alterTagImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //打开新窗口
                Intent selectTag = new Intent(AlterRecordActivity.this, SelectTagActivity.class);
                selectTag.putExtra("current_tag", currentTagStr);
                selectTag.putExtra("flag", "alter");
                startActivityForResult(selectTag, REQUEST_CODE_ALTER_TAG);
            }
        });

        //保存修改记录，跳转首页
        saveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String date = currentDateStr;
                String time = currentTimeStr;
                String title = titleText.getText().toString();

                if(title.equals("")){
                    //未填写标题——提示
                    Toast.makeText(AlterRecordActivity.this, R.string.unfilledTitleTip, Toast.LENGTH_SHORT).show();
                }
                else {

                    String detail = detailText.getText().toString();
                    String tag = currentTagStr;

                    Item item = new Item(date, time, title, detail, tag);
                    item.setId(currentId);
                    DBManager dbManager = new DBManager(AlterRecordActivity.this);
                    dbManager.update(item);
                    Log.i("db","修改记录");

                    Intent intent = new Intent(AlterRecordActivity.this, MainActivity.class);
                    startActivity(intent);
                    Log.i(TAG, "onClick: 修改记录，跳转首页");
                    finish();

                }

            }
        });


    }

    //接收传过来的时间和tag
    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        //requestCode是哪个窗口带回来的, resultCode表明窗口带回来的哪个数据
        if(requestCode == REQUEST_CODE_ALTER_TIME && resultCode == RESULT_CODE_ALTER_TIME){
            //接收修改后时间
            currentDateStr = data.getStringExtra("select_date");
            currentTimeStr = data.getStringExtra("select_time");
            String date_time = currentDateStr + " " +currentTimeStr;
            currentTime.setText(date_time);
            Log.i(TAG, "onActivityResult: date_time:" + date_time);

        }else if(requestCode == REQUEST_CODE_ALTER_TAG && resultCode == RESULT_CODE_ALTER_TAG) {
            //接收选择的标签
            currentTagStr = data.getStringExtra("select_tag");
            currentTag.setText(currentTagStr);
            Log.i(TAG, "onActivityResult: current_tag:" + currentTagStr);
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        if(item.getItemId()==android.R.id.home){
            finish();
            return true;
        }
        super.onOptionsItemSelected(item);
        return true;
    }

}